//
//  DefectConditionViewController.m
//  Audit
//
//  Created by cc on 15-2-15.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "DefectConditionViewController.h"

@interface DefectConditionViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    IBOutlet UITableView *_tableView;
    IBOutlet UISearchBar *_searchBar;
}
@end

@implementation DefectConditionViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavTitle:self.naviTitle];
    [self setNavBackButton:nil];
    [self setNavRightButton:@"关闭"];
    
    _tableView.delegate=self;
    
    if (_hiddenSearchBar) {
        _searchBar.hidden = YES;
        _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:animated];
    
    self.dataFromServerKey   = [NSMutableArray array];
    self.dataFromServerValue = [NSMutableArray array];
    
    if ([[self.paramsDic objectForKey:@"port"] isEqualToString:@"partList.do"]) {
        NSArray *keyArr=[NSArray arrayWithObjects:@"pageSize",@"currentIndex", nil];
        NSArray *valueArr=[NSArray arrayWithObjects:@"15",@"0", nil];
        
        [gAlert showLoading:nil delegate:nil];
        [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:[self.paramsDic objectForKey:@"port"] finishSelector:@selector(getDefectListFinished:) failSelector:@selector(getDefectListFailed:) delegateObj:self];
    }
    else if([[self.paramsDic objectForKey:@"port"] isEqualToString:@"locationList.do"]) {
        NSArray *keyArr=[NSArray arrayWithObjects:@"partList", nil];
        NSArray *valueArr=[NSArray arrayWithObjects:[self.paramsDic objectForKey:@"value"], nil];
        
        if (valueArr.count==0) {

            [gAlert show:@"还没有选择部件哦" inView:self.view delegate:nil];
        }
        else
        {
            [gAlert showLoading:nil delegate:nil];
            [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:[self.paramsDic objectForKey:@"port"] finishSelector:@selector(getDefectListFinished:) failSelector:@selector(getDefectListFailed:) delegateObj:self];
        }
    }
    else if([[self.paramsDic objectForKey:@"port"] isEqualToString:@"weightList.do"]) {
        NSArray *keyArr=[NSArray arrayWithObjects:@"defectList", nil];
        NSArray *valueArr=[NSArray arrayWithObjects:[self.paramsDic objectForKey:@"value"], nil];
        
        if (valueArr.count==0) {
            [gAlert show:@"还没有选择缺陷哦" inView:self.view delegate:nil];
        }
        else
        {
            [gAlert showLoading:nil delegate:nil];
            [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:[self.paramsDic objectForKey:@"port"] finishSelector:@selector(getDefectListFinished:) failSelector:@selector(getDefectListFailed:) delegateObj:self];
        }
    }
    else if([[self.paramsDic objectForKey:@"port"] isEqualToString:@"defectList.do"]\
            ||[[self.paramsDic objectForKey:@"port"] isEqualToString:@"zoneList.do"]\
            ||[[self.paramsDic objectForKey:@"port"] isEqualToString:@"respareaList.do"]) {
        [gAlert showLoading:nil delegate:nil];
        [CommunicateWithService communicateWithServiceApiName:[self.paramsDic objectForKey:@"port"] finishSelector:@selector(getDefectListFinished:) failSelector:@selector(getDefectListFailed:) delegateObj:self];
    }
}

-(void)getDefectListFinished:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    if (request.responseStatusCode == 400) {
        NSLog(@"Invalid code");
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
    } else if (request.responseStatusCode == 200) {
        NSDictionary *strDic=[CommunicateWithService getDataDic:request.responseString];//解析json 2 dic
        
        NSString *flag=[strDic objectForKey:@"resultCode"];//服务器返回代码
        
        NSString *msg=[strDic objectForKey:@"resultValue"];//服务器返回提示消息
        
        NSArray *strDicData=(NSArray *)[strDic objectForKey:@"resultData"];//服务器返回数据字典
        
        if ([flag isEqualToString:@"1001"]) {
            for (int i=0; i<strDicData.count; i++) {
                NSDictionary *dic=(NSDictionary *)[strDicData objectAtIndex:i];
                
                [self.dataFromServerKey addObject:[dic objectForKey:@"key"]];
                [self.dataFromServerValue addObject:[dic objectForKey:@"value"]];
            }
            
            [_tableView reloadData];
            
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"setIsLoadCarLsit" object:nil];
        }
        else if ([flag isEqualToString:@"1002"])
        {

            [gAlert show:msg inView:self.view delegate:nil];

            if (self.setText) {
                self.setText(@"",@"");
            }
        }
    }
}

-(void)getDefectListFailed:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    [NetworkCheck checkNetwork:self];//检查网络
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)navRightButtonActionAtTag:(NSUInteger)tag
{
    //todo:返回数据
    [super backAction:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchFlag) {
        return self.dataFromServerValueSearch.count;
    }
    else
    {
        return self.dataFromServerValue.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.font = [UIFont systemFontOfSize:18.0];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    if (self.searchFlag) {
        cell.textLabel.text = [self.dataFromServerValueSearch objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [self.dataFromServerValue objectAtIndex:indexPath.row];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.searchFlag)
    {
        if (self.setText) {
            self.setText([self.dataFromServerKeySearch objectAtIndex:[indexPath row]],[self.dataFromServerValueSearch objectAtIndex:[indexPath row]]);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
    else
    {
        if (self.setText) {
            self.setText([self.dataFromServerKey objectAtIndex:[indexPath row]],[self.dataFromServerValue objectAtIndex:[indexPath row]]);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

#pragma mark - UISearchBar
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar becomeFirstResponder];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText isEqualToString:@""]) {
        self.searchFlag = NO;
    }
    else
    {
        self.searchFlag = YES;
    }
    
    NSMutableArray *arr=[NSMutableArray array];
    
//    for (DetailPartData *data in self.detailPartInfoArr) {
//        [arr addObject:data.detailPartValue];
//    }
    
    self.dataFromServerKeySearch   = [NSMutableArray array];
    self.dataFromServerValueSearch = [NSMutableArray array];
    
    for (int i = 0;i < self.dataFromServerValue.count;i++) {
        if ([[self.dataFromServerValue objectAtIndex:i
                                 ] containsString:searchText]) {
            [self.dataFromServerValueSearch addObject:[self.dataFromServerValue objectAtIndex:i]];
            
            [self.dataFromServerKeySearch addObject:[self.dataFromServerKey objectAtIndex:i]];
        }
    }
    
    [_tableView reloadData];
}

@end
